<?php
/**
 * CRMLite Staff Controller
 *
 * @final
 * @package Crmlite_Module_Default
 * @version $Id: StaffController.php 62 2007-09-10 06:40:30Z etng2004 $
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/AdminBaseController.php';
final class Admin_StaffController extends AdminBaseController
{
     /**
	 * 默认的模型
	 *
	 * 未初始化时是字符串，初始化之后就是一个模型接口
	 *
	 * @var StaffModel
	 */
    protected $model = 'Staff';
	public function init()
	{
		parent::init();
		$this->pushPoint('员工管理');

	}

	public function preDispatch()
	{
		if($this->S_staff['ROLE'] != 'ADMIN')
		{
		    $this->feedback('只有系统管理员才能进行员工管理', '失败', $this->module_url);
		}
	}
	public function indexAction()
	{
		$this->log('Index page called!');
		$this->_forward('list');
	}
	public function listAction()
	{
	    $where = array("id<>{$this->S_staff_id}");
	    $order = array('created_at DESC');
		$pager = array(
		'curpage' 	=> $this->_getParam('page', 1),
		'perpage'	=> 10,
		'total'		=> $this->model->count($where),
		'url'		=> $this->base_url . '/list',
		);
		$ClientTypes = new ClientTypeModel();
		$clienttypes = $ClientTypes->asPairs('code', 'name');
		$default_statics = array_fill_keys(array_keys($clienttypes), 0);
		$this->view->statics = $this->model->statics($where, 'client_type', $default_statics);
		$this->view->staffs = $this->model->paginate($where, $order, $pager['curpage'], $pager['perpage'] );
		$this->view->pager = $pager;
		$this->setTitle('员工列表', $pager['total']);

	}
	public function batchOperateAction()
	{
		/**
    	 * WE do not need render from template!
    	 */
		$this->_helper->viewRenderer->setNoRender();
		if($this->isPost())
		{
			var_dump($_POST);
			echo "todo";
		}
	}
	public function addAction()
	{
		if($this->isPost())
		{
			$record = $_POST['fields'];
			$record['created_at'] = time();
			if($this->model->insert($record))
			{
				$this->removeCache();
				$this->log($msg = "员工 #{$record['id']} 添加成功!");
				$this->feedback($msg, '成功', $this->base_url);
			}
			$this->view->staff = $record;
		}
		$this->setTitle('新增员工');
	}
	public function editAction()
	{
		if($this->isPost())
		{
			$record = $_POST['fields'];
			$id = $record['id'];
			$record['updated_at'] = time();
			if($this->model->update($record, "id={$id}"))
			{
				$this->removeCache();
				$this->log($msg = "Contact #{$record['id']} updated!");
				$this->feedback($msg, '成功', $this->base_url);
			}
			$this->view->staff = $record;
			$error = 'Update failed!';
		}
		/**
    	 * 若不是POST方法则为初始编辑
    	 */
		$this->setTitle('编辑员工资料');
		if(empty($error))
		{
			$id = intval($this->_getParam('id', 0));
			$this->view->staff = $this->model->find($id);
		}
	}
	public function viewAction()
	{
		$id = intval($this->_getParam('id', 1));
		$this->view->staff =$staff = $this->model->find($id);
		$this->setTitle($staff['name']);
	}

	public function dummyAction()
	{

	}

	public function dummy2Action()
	{

	}
	/**
	 * 删除指定的记录
	 *
	 */
	public function removeAction()
	{
		$id = intval($this->_getParam('id', 1));
		if($id)
		{
			if($status = $this->model->deleteById($id))
			{
				$this->removeCache();
				$this->log($msg = "Contact #{$id} deleted!");
				$this->feedback($msg, '成功', $this->base_url);
			}
		}
		echo $status;
		exit();
	}

	public function profileAction()
	{
		$this->view->staff =$staff = $this->model->find($this->S_staff_id);
		$this->setTitle("个人资料");
	}
	/**
     * 删除缓存
     *
     * 如果不指定记录ID，则默认删除所有相应的缓存
     *
     * @param int $id
     * @todo 实现删除所有的功能
     */
	protected function removeCache( $id = null )
	{
		$Cache = Zend_Registry::get('cache');
		/* @var $Cache Zend_Cache_Core */
		$Cache->remove('admin_announce_feed_data');
		$Cache->remove('announce_feed_data');
	}
}
